package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/9/25
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid",defaultValue = "0")Long pid) {
        List<Category>categoryList=  categoryService.queryByParentId(pid);
        if (categoryList==null||categoryList.size()==0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(categoryList);
    }
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrand(@PathVariable("bid")Long bid) {
        List<Category>categoryList=  categoryService.queryByBrandId(bid);
        if (categoryList==null||categoryList.size()==0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(categoryList);
    }
    /**
     * 根据组件删除,
     * @param id
     * @return
     */
    @DeleteMapping("list")
    public ResponseEntity<Void> deleteById(@RequestParam(value = "id")Long id) {
        try {
            categoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 根据分类的id插叙三级分类对应的名称
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids) {
        List<String> names = categoryService.queryCategoryNames(ids);
        if (names==null||names.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.CONTINUE);
        }
        return ResponseEntity.ok(names);
    }

    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllById3(@RequestParam("id")Long id) {

        List<Category>categories= this.categoryService.queryAllById3(id);
        if (categories == null || categories.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(categories);
    }


}
